package com.wanghlme.bookstore.exception;

import com.alibaba.fastjson.JSONObject;

/**
 * @author WangHailong
 */
public class CustomResult {
    private String code;
    private String message;
    private Object result;

    public CustomResult() {
    }

    public CustomResult(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public static CustomResult success() {
        return success(null);
    }

    public static CustomResult success(Object data) {
        CustomResult ghr = new CustomResult();
        ghr.setCode(ErrorInfoEnum.SUCCESS.getResultCode());
        ghr.setMessage(ErrorInfoEnum.SUCCESS.getResultMsg());
        ghr.setResult(data);
        return ghr;
    }

    public static CustomResult failure(BaseErrorInfoInterface errorInfo) {
        CustomResult ghr = new CustomResult();
        ghr.setCode(errorInfo.getResultCode());
        ghr.setMessage(errorInfo.getResultMsg());
        ghr.setResult(null);
        return ghr;
    }

    public static CustomResult failure(String code, String msg) {
        CustomResult ghr = new CustomResult();
        ghr.setCode(code);
        ghr.setMessage(msg);
        ghr.setResult(null);
        return ghr;
    }

    public static CustomResult failure(String msg) {
        CustomResult ghr = new CustomResult();
        ghr.setCode("-1");
        ghr.setMessage(msg);
        ghr.setResult(null);
        return ghr;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
